package com.petalsgroup.petals.service.Inform.impl;

import com.petalsgroup.petals.dao.Comment.CommentDao;
import com.petalsgroup.petals.dao.Comment.Impl.CommentDaoImpl;
import com.petalsgroup.petals.dao.admin.AdminDao;
import com.petalsgroup.petals.dao.admin.impl.AdminDaoImpl;
import com.petalsgroup.petals.dao.bannedTime.BannedTimeDao;
import com.petalsgroup.petals.dao.bannedTime.impl.BannedTimeDaoImpl;
import com.petalsgroup.petals.dao.comInform.ComInformDao;
import com.petalsgroup.petals.dao.comInform.impl.ComInformDaoImpl;
import com.petalsgroup.petals.dao.disInform.DisInformDao;
import com.petalsgroup.petals.dao.disInform.impl.DisInformDaoImpl;
import com.petalsgroup.petals.dao.discuss.DiscussDao;
import com.petalsgroup.petals.dao.discuss.impl.DiscussDaoImpl;
import com.petalsgroup.petals.dao.reason.ReasonDao;
import com.petalsgroup.petals.dao.reason.impl.ReasonDaoImpl;
import com.petalsgroup.petals.dao.user.UserDao;
import com.petalsgroup.petals.dao.user.impl.UserDaoImpl;
import com.petalsgroup.petals.pojo.BannedTimeDO;
import com.petalsgroup.petals.pojo.ReasonDO;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.Inform.InformService;
import com.petalsgroup.petals.util.RecentlyUtil;

import java.util.Date;
import java.util.List;

public class InformServiceImpl implements InformService {
    DisInformDao did=new DisInformDaoImpl();
    ComInformDao cid=new ComInformDaoImpl();
    CommentDao cmd=new CommentDaoImpl();
    DiscussDao dcd=new DiscussDaoImpl();
    UserDao ud = new UserDaoImpl();
    BannedTimeDao btd = new BannedTimeDaoImpl();
    AdminDao ad=new AdminDaoImpl();
    ReasonDao rd=new ReasonDaoImpl();

    /**
     * 查看该用户是否已经举报过该评论
     * @param com_id
     * @param uid
     * @return
     * @author wgd
     */
    @Override
    public Boolean findComInform(String com_id, String uid) {
        Boolean flag = cid.comInformAgain(Integer.valueOf(com_id), Integer.valueOf(uid));
        System.out.println(flag);
        return flag;
    }

    /**
     * 将举报记录插入举报记录表中
     * @param dis_id
     * @param uid
     * @param iid
     * @author wgd
     */
    @Override
    public void addComInform(String dis_id, String uid, String iid) {
        //先通过com_id找uided
        Integer uided =cmd.findUidByComId(Integer.parseInt(dis_id));
        cid.addComInform(dis_id,uid, String.valueOf(uided),iid);
        Integer userInformNum = ud.getUserInformNumByUid(uided);
        if (userInformNum>=9){
            //如果当前举报总等于9就将当前总数请0，载进行判断，是否已经被禁言，如果已经被禁言，那么取到解封时间，增加三天，再更新数据，否者创建新的解封记录
            ud.updateUserInformNumByUid(uided,0);
            String openTime=null;
            if (btd.findUserByUid(uided)){
                Date time=btd.getOpenTimeByUid(uided);
                openTime = RecentlyUtil.getTimeAddStr(time,-3);
                btd.addOpenTimeByUided(uided,openTime);
            }
            else {
                openTime=RecentlyUtil.getRecentlyDayTimeStr(-3);
                btd.insertOpenTime(uided,openTime);
            }
            ad.bannedUser(String.valueOf(uided));
        }//如果没有到达就加一,并更新到数据库
        else {
            userInformNum++;
            ud.updateUserInformNumByUid(uided,userInformNum);
        }
    }


    /**
     * 查看该用户是否已经举报过该回复
     * @param dis_id
     * @param uid
     * @return
     * @author wgd
     */
    @Override
    public Boolean findDisInform(String dis_id, String uid) {
        Boolean flag=did.disInformAgain(Integer.valueOf(dis_id),Integer.valueOf(uid));
        return flag;
    }
    /**
     * 创建回复信息的举报记录
     * @param dis_id
     * @param uid
     * @param iid
     * @author wgd
     */
    @Override
    public void addDisInform(String dis_id, String uid, String iid) {
        //dis_id
        Integer uided =dcd.findUidByDid(Integer.parseInt(dis_id));
        did.addDisInform(dis_id,uid, String.valueOf(uided),iid);
        Integer userInformNum = ud.getUserInformNumByUid(uided);
        if (userInformNum>=9){
            //如果当前举报总等于9就将当前总数请0，载进行判断，是否已经被禁言，如果已经被禁言，那么取到解封时间，增加三天，再更新数据，否者创建新的解封记录
            ud.updateUserInformNumByUid(uided,0);
            String openTime=null;
            if (btd.findUserByUid(uided)){
                Date time=btd.getOpenTimeByUid(uided);
                openTime = RecentlyUtil.getTimeAddStr(time,-3);
                btd.addOpenTimeByUided(uided,openTime);
            }
            else {
                openTime=RecentlyUtil.getRecentlyDayTimeStr(-3);
                btd.insertOpenTime(uided,openTime);
            }
            ad.bannedUser(String.valueOf(uided));
        }//如果没有到达就加一,并更新到数据库
        else {
            userInformNum++;
            ud.updateUserInformNumByUid(uided,userInformNum);
        }
    }

    /**
     * 解禁禁言时间以及到了的用户
     * @param uid
     */
    @Override
    public void rosolveUserById(Integer uid) {
        ad.resolveUser(String.valueOf(uid));
    }

    /**
     * 获取所有用户的禁言时间
     * @return
     */
    @Override
    public List<BannedTimeDO> findAllOpenTimeList() {
        List<BannedTimeDO> list = btd.getAllOpenTime();
        return list;
    }

    @Override
    public List<ReasonDO> getReason() {
        List<ReasonDO> list = rd.getAllReason();
        return list;
    }


}
